type AvatarOptions = {
  mouth: number
  glasses: number
  hairColor: number
  hair: number
}

const useAvatar = () => {
  const hairs = [
    ...range(1, 19).map((v: number) => `short${padStart(v, 2, '0')}`),
    ...range(1, 27).map((v: number) => `long${padStart(v, 2, '0')}`)
  ]
  const hairColors = [
    'e16162', //红
    '0e0e0e', //黑
    'fec7d7', //粉
    'ffd803', //黄
    '3da9fc', //蓝
    'd8eefe', //浅蓝,
    '7f5af0', //紫
    'bae8e8'
  ]
  const mouths = range(1, 31).map((v: number) => `variant${padStart(v, 2, '0')}`)
  const glasses = range(1, 4).map((v: number) => `variant${padStart(v, 2, '0')}`)
  const basicUrl = 'https://api.dicebear.com/7.x/adventurer/svg'
  const defaultProps = {
    backgroundColor: 'ffffff',
    skinColor: 'f2d3b1',
    scale: '100',
    glassesProbability: '100',
    translateY: '5'
  }

  const lists = [
    { key: 'hair', list: hairs },
    { key: 'hairColor', list: hairColors },
    { key: 'mouth', list: mouths },
    { key: 'glasses', list: glasses }
  ]
  const getRandom = (options?: any) => {
    const randomParams = {
      hair: sample(hairs),
      hairColor: sample(hairColors),
      mouth: sample(mouths),
      glasses: sample(glasses)
    }
    const params = { ...defaultProps, ...randomParams, ...options }
    return `${basicUrl}?${new URLSearchParams(params).toString()}`
  }

  const getAvatar = (options: Partial<AvatarOptions>) => {
    const params = {
      ...defaultProps,
      ...mapValues(pickBy(options), (v: number, k: string) => {
        const list = get(find(lists, { key: k }), 'list')
        return get(list, v - 1)
      })
    }
    const url = `${basicUrl}?${new URLSearchParams(params).toString()}`
    return url
  }

  return { lists, basicUrl, defaultProps, getRandom, getAvatar }
}

export default useAvatar
